/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EventsPanel.java
 *
 * Created on Dec 16, 2010, 10:21:42 PM
 */
package net.sourceforge.slcwsn.monitorapp;

import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import net.sourceforge.slcwsn.event.Event;
import net.sourceforge.slcwsn.event.TransientEvent;
import net.sourceforge.slcwsn.network.Network;
import net.sourceforge.slcwsn.network.NetworkObserver;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class EventsPanel extends javax.swing.JPanel implements NetworkObserver {

    private Network network;
    private DefaultListModel model = new DefaultListModel();

    /** Creates new form EventsPanel */
    public EventsPanel(Network network) {
        this.network = network;
        initComponents();
        jList1.setModel(model);
        jList1.setCellRenderer(new DefaultListCellRenderer() {

            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Event event = (Event) value;
                EventShortDescriptor descriptor = new EventShortDescriptor();
                event.accept(descriptor);
                return super.getListCellRendererComponent(list, descriptor.getDescription(), index, isSelected, cellHasFocus);
            }
        });
        network.getObservers().add(this);
        jList1.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                Event event = (Event) jList1.getSelectedValue();
                EventLongDescriptor descriptor = new EventLongDescriptor();
                event.accept(descriptor);
                jEditorPane1.setText(descriptor.getDescription());
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();

        jSplitPane1.setDividerLocation(200);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setResizeWeight(0.5);

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jList1);

        jSplitPane1.setTopComponent(jScrollPane1);

        jEditorPane1.setContentType("text/html");
        jEditorPane1.setEditable(false);
        jScrollPane2.setViewportView(jEditorPane1);

        jSplitPane1.setRightComponent(jScrollPane2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    // End of variables declaration//GEN-END:variables

    public void newEvent(Event event) {
        if (!(event instanceof TransientEvent)) {
            model.addElement(event);
            this.validate();
            this.repaint();
            jList1.repaint();
        }
    }
}
